package com.ll.beautymall.ui.quality.view;


import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.ll.beautymall.R;
import com.ll.beautymall.adapter.ZeroAdapter;
import com.ll.beautymall.api.UrlConfig;
import com.ll.beautymall.base.BaseFragment;
import com.ll.beautymall.bean.ZeroData;
import com.ll.beautymall.ui.quality.presenter.QualityPresenter;
import com.ll.beautymall.utils.NetUtil;

import java.util.HashMap;

import butterknife.BindView;


/**
 * A simple {@link Fragment} subclass.
 */
public class QualityFragment extends BaseFragment implements IQualityView {


    @BindView(R.id.xrv_quality)
    XRecyclerView xrvQuality;

    private QualityPresenter qualityPresenter;
    private String previous_cursor = "0";
    private String next_cursor;
    private ZeroAdapter qualityAdapter;
    private HashMap<String, String> parameters;


    public QualityFragment() {
    }

    @Override
    protected void initView(ViewGroup container) {
        qualityPresenter = new QualityPresenter(this);
        parameters = new HashMap<>();

        configXRecyclerView();


        parameters.put(UrlConfig.Key.CURSOR, String.valueOf(previous_cursor));
        parameters.put(UrlConfig.Key.COUNT, "40");

        loadQualityData(false);

        xrvQuality.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                loadQualityData(false);
                xrvQuality.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                parameters.put("cursor", String.valueOf(next_cursor));
                loadQualityData(true);
                xrvQuality.loadMoreComplete();
            }
        });
    }

    private void loadQualityData(boolean isMore) {
        if (NetUtil.hasInternet(getFActivity())) {
            qualityPresenter.setQualityData(isMore, parameters);
        }
    }

    private void configXRecyclerView() {
        ImageView iv_empty = new ImageView(getFActivity());
        iv_empty.setBackgroundResource(R.mipmap.img_begin_05);
        xrvQuality.setEmptyView(iv_empty);

        RecyclerView.LayoutManager manager = new GridLayoutManager(getFActivity(), 2, GridLayoutManager.VERTICAL, false);
        xrvQuality.setLayoutManager(manager);

        qualityAdapter = new ZeroAdapter(getFActivity());
        xrvQuality.setAdapter(qualityAdapter);
    }

    @Override
    public int getFLayoutId() {
        return R.layout.fragment_quality;
    }

    @Override
    public void setQualityData(ZeroData zeroData) {
        next_cursor = zeroData.getNext_cursor();
        previous_cursor = zeroData.getPrevious_cursor();
        qualityAdapter.setData(zeroData.getProducts());
    }

    @Override
    public void addQualityData(ZeroData zeroData) {
        next_cursor = zeroData.getNext_cursor();
        previous_cursor = zeroData.getPrevious_cursor();
        qualityAdapter.addData(zeroData.getProducts());
    }

    @Override
    public void showErrorMsg(String errorMsg) {
        Toast.makeText(getFActivity(), errorMsg, Toast.LENGTH_SHORT).show();
    }

}
